<?php
/**
 * CourseController.class.php
 * 课程控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-02-24
 */
namespace Admin\Controller;

class CourseController extends BaseController {

	/**
	 * 获取课程列表
	 * @param   $p 页码数
	 * @return  json
	 */
	function getList(){
		$dateS 	= $this->request('date_start', false);
		$timeS 	= $dateS ? strtotime($dateS) : false;
		$dateE 	= $this->request('date_end', false);
		$timeE 	= $dateE ? strtotime($dateE) + 3600*24 : false;

		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		$pageHtml 	= true;
		$teachers 	= D('Teacher')->getList(false, $pageHtml);

		$list 	= D('Course')->getList($timeS, $timeE, $page, $pageHtml);
		if($list === false)
			$this->outOInfo(D('Course')->errMsg, 0);

		$this->outOInfo(array('list' => $list, 'page_html' => $pageHtml, 'teachers' => $teachers));
	}

	/**
	 * 添加课程
	 * @param   $name 课程名称
	 * @param   $m_title 分享主标题
	 * @param   $s_title 分享副标题
	 * @param   $banner banner图路径
	 * @param   $decipt 描述
	 * @param   $teacher_id 教师ID
	 * @param   $price  购买价格
	 * @param   $score 购买积分
	 * @param   $buy_type 购买方式
	 * @param   $tag 标签
	 */
	function addCourse(){
		$this->unEmptyParam(['name', 'main_title', 'sub_title', 'banner', 'decipt', 'teacher_id',
			'buy_type', 'cate']);

		$courseInfo 	= [
			'name'	=> $this->request('name'),
			'main_title'	=> $this->request('main_title'),
			'sub_title'	=> $this->request('sub_title'),
			'banner'	=> $this->request('banner'),
			'decipt'	=> $this->request('decipt'),
			'teacher_id'	=> $this->request('teacher_id'),
			'buy_type' 	=> $this->request('buy_type'),
			'cate'	=> $this->request('cate'),
			'price'	=> $this->request('price', 0),
			'score'	=> $this->request('score', 0),
			'tag'	=> $this->request('tag', 0)
		];
		if($courseInfo['banner'])
			$courseInfo['banner']	= strchr($courseInfo['banner'], '/server');

		$ret 	= D('Course')->addCourse($courseInfo);
		if($ret === false)
			$this->outOInfo(D('Coruse')->errMsg, 0);
		$this->outOInfo();
	}

	/**
	 * 变更课程
	 * @param   $course_id 	课程ID
	 * @param   $course_info 	课程信息
	 * @return  json
	 */
	function changeCourse(){
		$this->unEmptyParam(['course_id', 'course_info']);
		
		$courseId 	= $this->request('course_id');
		$courseInfo 	= $this->request('course_info');

		//字段转义
		if($courseInfo['banner'])
			$courseInfo['banner']	= strchr($courseInfo['banner'], '/server');

		$result 	= D('Course')->changeCourse($courseId, $courseInfo);
		if($result === false)
			$this->outOInfo(D('Course')->errMsg, 0);
		$this->outOInfo();
	}

	/**
	 * 获取课程详情
	 * @param   $course_id 课程id
	 * @return  $courseInfo 课程详情
	 */
	function detailCourse(){
		$courseId 	= $this->iInfo['course_id'];
		$courseInfo 	= D('Course')->detailCourse($courseId);
		if($courseInfo === false)
			$this->outOInfo(D('Course')->errMsg);
		$this->outOInfo(array('info' => $courseInfo));
	}

	/**
	 * 获取课程章节列表
	 * @param   $course_id 课程ID
	 * @return  list [<description>]
	 */
	function getChapterList(){
		$this->unEmptyParam(array('course_id'));
		$courseId 	= $this->iInfo['course_id'];
		$list 	= D('Course')->getChapterList($courseId);
		if($list === false)
			$this->outOInfo(D('Course')->errMsg, 0);
		$this->outOInfo(array('list' => $list));
	}

	/**
	 * 添加章节
	 * @param   $course_id 课程ID
	 * @param   $name 章节名称
	 * @param   $decipt 章节描述
	 * @param   $imgs ppt图片序列
	 * @param   $audio 音频路径
	 * @param   $time 音频时长
	 * @param   $attach 学习资料
	 * @param   $is_try 是否试听
	 * @return  $boolean
	 */
	function addChapter(){
		$this->unEmptyParam(array('course_id', 'name', 'decipt', 'imgs', 'audio', 'is_try'));
		$imgs 	= $this->iInfo['imgs'];
		foreach($imgs as &$img){
			$img['path'] 	= strchr($img['path'], '/server');
		}
		$imgs 	= json_encode($imgs);
		$audio  = $this->iInfo['audio'];
		$audio 	= strchr($audio, '/server');
		$time 	= get_audio_info(strchr($audio, 'Uploads'), 'playtime_seconds');

		$chapterInfo 	= array(
			'course_id'	=> $this->iInfo['course_id'],
			'name'	=> $this->iInfo['name'],
			'decipt'	=> $this->iInfo['decipt'],
			'imgs'	=> $imgs,
			'audio'	=> $audio,
			'time'	=> $time,
			'attach'	=> $this->iInfo['attach'],
			'is_try'	=> $this->iInfo['is_try']
		);

		$ret 	= D('Course')->addChapter($chapterInfo);
		if($ret === false)
			$this->outOInfo(D('Course')->errMsg, 0);
		$this->outOInfo('Oh yes!');
	}

	/**
	 * 变更章节信息
	 * @param   $chapter_id 章节ID
	 * @param   $name 章节名称
	 * @param   $decipt 描述
	 * @param   $imgs 图片路径
	 * @param   $time 音频时长
	 * @param   $audio 音频路径
	 * @param   $attach 学习资料
	 * @param   $is_try 是否试听
	 * @return  $boolean [<description>]
	 */
	function changeChapter(){
		$this->unEmptyParam(array('chapter_id'));
		$chapterId 	= $this->iInfo['chapter_id'];
		if($this->iInfo['name'])
			$chapterInfo['name']	= $this->iInfo['name'];
		if($this->iInfo['decipt'])
			$chapterInfo['decipt']	= $this->iInfo['decipt'];
		if($this->iInfo['audio']){
			$chapterInfo['audio']	= strchr($this->iInfo['audio'], '/server');
			$chapterInfo['time'] 	= get_audio_info(strchr($chapterInfo['audio'], 'Uploads'), 'playtime_seconds');
		}
		if($this->iInfo['imgs']){
			$imgs 	= $this->iInfo['imgs'];
			foreach($imgs as &$img){
				$img['path']	= strchr($img['path'], '/server');
			}
			$chapterInfo['imgs']	= json_encode($imgs);
		}
		if($this->iInfo['attach'])
			$chapterInfo['attach']	= $this->iInfo['attach'];
		if(isset($this->iInfo['is_try']))
			$chapterInfo['is_try']	= $this->iInfo['is_try'];
		if(isset($this->iInfo['status']))
			$chapterInfo['status']	= $this->iInfo['status'];
		
		if($chapterInfo){
			$ret 	= D('Course')->changeChapter($chapterId, $chapterInfo);
			if($ret === false)
				$this->outOInfo(D('Course')->errMsg);
		}
		$this->outOInfo('Oh yes!');
	}

	/**
	 * 销售记录
	 * @param   $p 页码数
	 * @param   $keyword 关键词
	 * @param   $get_type 获取方式
	 * @param   $date_start 开始时间
	 * @param   $date_end 结束时间
	 */
	function sellLog(){
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		$keyword 	= $this->iInfo['keyword'];
		$getType 	= $this->iInfo['get_type'];
		$timeS 	= $this->iInfo['date_start'] ? strtotime($this->iInfo['date_start']) : false;
		$timeE 	= $this->iInfo['date_end']	? strtotime($this->iInfo['date_end'].' +1 day') : false;

		$pageHtml 	= '';
		$list 	= D('Course')->sellLog($page, $keyword, $getType, $timeS, $timeE, $pageHtml);
		if($list === false)
			$this->outOInfo(D('Course')->errMsg, 0);

		$this->outOInfo(array('list' => $list, 'page_html'=> $pageHtml)); 
	}

	/**
	 * 现金交易记录
	 * @param   $p 页码数
	 * @param   $keyword 关键词
	 * @param   $date_start 开始时间
	 * @param   $date_end 结束时间
	 * @param 	$data_type 	获取数据类型 1：单笔购买 2：购物车购买
	 * @return  $list json [<description>]
	 */
	function getBargainList(){
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		$keyword 	= $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;
		$timeS 	= $this->iInfo['date_start'] ? strtotime($this->iInfo['date_start']) : false;
		$timeE 	= $this->iInfo['date_end']	? strtotime($this->iInfo['date_end'].' +1 day') : false;
		$dataType 	= $this->iInfo['data_type'] ? $this->iInfo['data_type'] : 1;

		$pageHtml 	= true;
		$total 	= true;
		$list 	= D('Course')->getBargainList($dataType, $keyword, $timeS, $timeE, $page, $pageHtml, $total);
		if($list === false)
			$this->outOInfo(D('Course')->errMsg, 0);
		$this->outOInfo(array('list' => $list, 'page_html' => $pageHtml, 'total' => $total));
	}

	/**
	 * 导出交易记录
	 * @param   $start 页码数
	 * @param   $length [<description>]
	 * @param   $keyword 关键词
	 * @param   $date_start 开始时间
	 * @param   $date_end 结束时间
	 * @return  $list json [<description>]
	 */
	function outBargainExcel(){
		$start  = isset($this->iInfo['start']) ? $this->iInfo['start'] : false;
        $length     = $this->iInfo['length'] ? $this->iInfo['length'] : 1000;
        $keyword    = $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;
        $timeS  = $this->iInfo['date_start'] ? 
            strtotime($this->iInfo['date_start']) : false;
        $timeE  = $this->iInfo['date_end'] ? 
            strtotime($this->iInfo['date_end'].' +1 day') : false;

        $data   = D('Course')->outBargainExcel($start, $length, $keyword, $timeS, $timeE);
        if(is_numeric($data))
            $this->outOInfo(array('count' => $data));

        $title  = array(
            '订单ID', '用户ID', '用户昵称', '真实姓名', '用户角色', '课程名称', '标准价格', '购买价格', 
            '分成金额', '获得利润', '交易单号', '优惠码', '优惠金额'
        );
        foreach($data as &$item){
            $d['money_id']    = $item['money_id'];
            $d['user_id']	= $item['user_id'];
            $d['nickname']  = $item['nickname'];
            $d['truename']  = $item['truename'];
            $d['role_text'] = $item['role_text'];
            $d['course_name']    = $item['course_name'];
            $d['price']   = $item['price'];
            $d['amount']    = $item['amount'];
            $d['deduct']  = $item['deduct'];
            $d['profit']    = $item['profit'];
            $d['trade_no']    = $item['trade_no'];
            $d['code']   = $item['code'];
            $d['code_amount']   = $item['code_amount'];
            $item   = $d;
        }
        out_excel($title, $data, 'course_list_'.$start);
	}

	/**
	 * 详细利益分配信息
	 * @param   $get_type 课程获取方式
	 * @param   $get_relate_id 关联ID
	 * @return  $info
	 */
	function detailBargainInfo(){
		$this->unEmptyParam(array('get_type', 'get_relate_id'));
		$getType 	= $this->iInfo['get_type'];
		$getRelateId 	= $this->iInfo['get_relate_id'];
		if(in_array($getType, array(1,2,4))){
			$moneyId 	= $getRelateId;
		}
		if($getType == 3){
			$moneyId 	= M('t_shop_cart', null)
				->where('id = %d', $getRelateId)
				->getField('money_id');
		}
		if(!$moneyId)	$this->outOInfo('无法获取该类型交易信息!');

		$dInfo 	= D('Deduct')->detailInfo($moneyId);
		if($dInfo === false)
			$this->outOInfo(D('Deduct')->errMsg, 0);

		$mInfo 	= D('Money')->detailInfo($moneyId);
		if($mInfo === false)
			$this->outOInfo(D('Money')->errMsg, 0);

		$this->outOInfo(array('deduct_list' => $dInfo, 'money_info' => $mInfo));
	}

}